package com.ol.edu.server.controller.advertise;


import com.github.pagehelper.PageInfo;
import com.ol.edu.server.entity.PromotionSpace;
import com.ol.edu.server.entity.ResponseResult;
import com.ol.edu.server.service.advertise.PromotionSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Roger
 * @date 2020/9/7 10:16
 * @description
 */
@RestController
@RequestMapping("/PromotionSpace")
@CrossOrigin
public class PromotionSpaceController {

    @Autowired
    private PromotionSpaceService promotionSpaceService;
    /*
        获取广告位列表数据
     */
    @RequestMapping("/findAllPromotionSpace")
    public ResponseResult findAllPromotionSpace(Integer currentPage, Integer pageSize) {
        List<PromotionSpace> promotionSpaceList = promotionSpaceService.findAllPromotionSpace();
        return new ResponseResult(true, 200, "查询所有广告位数据成功", promotionSpaceList);
    }

    /*
        获取广告位列表数据
     */
    @RequestMapping("/findAllPromotionSpaceByPage")
    public ResponseResult findAllPromotionSpaceByPage(Integer currentPage, Integer pageSize) {
        PageInfo<PromotionSpace> pageInfo = promotionSpaceService.findAllPromotionSpaceByPage(currentPage, pageSize);
        return new ResponseResult(true, 200, "查询所有广告位数据成功", pageInfo);
    }

    /*
        添加&修改广告位
     */
    @RequestMapping("/saveOrUpdatePromotionSpace")
    public ResponseResult saveOrUpdatePromotionSpace(@RequestBody PromotionSpace promotionSpace) {
        //如果传入的ID为空表示新增
        if (promotionSpace.getId() == null) {
            promotionSpaceService.savePromotionSpace(promotionSpace);
            return new ResponseResult(true, 200, "添加广告位成功", null);
        } else { //更新
            promotionSpaceService.updatePromotionSpace(promotionSpace);
            return new ResponseResult(true, 200, "修改广告位成功", null);
        }
    }
    /*
        回显广告位名称
     */
    @RequestMapping("/findPromotionSpaceById")
    public ResponseResult findPromotionSpaceById(Integer id) {
        PromotionSpace promotionSpace = promotionSpaceService.findPromotionSpaceById(id);
        return new ResponseResult(true, 200, "查询成功", promotionSpace);
    }


}
